package com.dodoke.pages;

import com.dodoke.entity.Account;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {
        //系统需要的容器对象，用于存储账户对象
        ArrayList<Account> accounts = new ArrayList<>();
        accounts.add(new Account("12345678","莫兰特","123456",500,3000));
        accounts.add(new Account("23456789","库里","234567",600,4000));
        accounts.add(new Account("34567891","哈登","345678",700,5000));

        //准备系统的首页：登录，开户
        showMain(accounts);
    }

    public static void showMain(ArrayList<Account> accounts) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎您进入首页");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("请您输入您想做的操作：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    //登录
                    login(accounts, sc);
                    break;
                case 2:
                    //开户
                    register(accounts, sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持！");
            }
        }
    }

    //登录
    private static void login(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("欢迎你进入登录界面");
        if (accounts.size() > 0) {
            while (true) {
                System.out.println("请您输入登录的卡号：");
                String cardId = sc.next();
                Account acc = getAccountByCardId(cardId, accounts);
                //根据卡号去集合中查询是否存在账户对象
                if (acc != null) {
                    while (true) {
                        //判断密码是否正确
                        System.out.println("请您输入账户密码");
                        String password = sc.next();
                        if (acc.getPassWord().equals(password)) {
                            System.out.println("欢迎您：" + acc.getUserName() + "进入系统，您可以开始办理你的业务了!");
                            showCommand(sc, acc, accounts);
                            return;
                        }
                    }
                } else {
                    System.out.println("卡号不存在，请确认");
                }
            }
        } else {
            System.out.println("当前系统无任何账户，请先注册再登录");
        }
    }
    //开户
    private static void register(ArrayList<Account> accounts, Scanner sc) {
        //创建一个账户对象封装账户信息
        Account acc = new Account();
        System.out.println("欢迎您进入开户界面");
        //录入用户账户信息
        System.out.println("请您输入账户名称：");
        String useName = sc.next();
        acc.setUserName(useName);

        while (true) {
            System.out.println("请您输入账户密码：");
            String password = sc.next();
            System.out.println("请您输入确认密码：");
            String okPassword = sc.next();

            //判断两次输入的密码是否一致
            if (okPassword.equals(password)) {
                acc.setPassWord(okPassword);
                break;
            } else {
                System.out.println("两次输入的密码不一致");
            }
        }

        System.out.println("请您设置当前取现额度：");
        double quotaMoney = sc.nextDouble();
        acc.setQuotaMoney(quotaMoney);
        // 关键点：为当前账户生成一个随机的8位数字作为卡号，卡号不能与其他用户的卡号重复。
        String cardId = createCardId(accounts);
        acc.setCardId(cardId);

        //把账户对象存入到集合容器对象中去
        accounts.add(acc);
        System.out.println("恭喜您，" + acc.getUserName() + "，您已经完成开户，您的卡号为：" + acc.getCardId());
    }

    //新建卡号
    private static String createCardId(ArrayList<Account> accounts) {
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 1; i <= 8; i++) {
                cardId = cardId + r.nextInt(10);
            }
            // 判断这个卡号是否重复：根据卡号去查询账户对象
            Account account = getAccountByCardId(cardId, accounts);
            if (account == null) {
                return cardId;
            }
        }
    }

    //获取卡号
    private static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
        for (Account acc : accounts) {
            if (acc.getCardId().equals(cardId)) {
                return acc;
            }
        }
        return null;
    }

    //操作界面
    private static void showCommand(Scanner sc, Account acc, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("欢迎您进入操作界面");
            System.out.println("1、查询");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.println("7、注销账户");
            System.out.println("请您输入操作命令：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    //查询账户信息
                    showAccount(acc);
                    break;
                case 2:
                    // 存款
                    depositMoney(acc, sc);
                    break;
                case 3:
                    // 取款
                    drawMoney(acc, sc);
                    break;
                case 4:
                    // 转账
                    transferMoney(acc, accounts, sc);
                    break;
                case 5:
                    // 修改密码
                    updatePassword(acc, sc);
                    return;
                case 6:
                    // 退出 回到首页
                    System.out.println("欢迎下次光临");
                    return;
                case 7:
                    // 注销账户
                    accounts.remove(acc);
                    System.out.println("您的账户已注销，将返回首页");
                    return;
                default:
                    System.out.println("您的操作命令有误！");
            }
        }
    }
    //取款
    private static void drawMoney(Account acc, Scanner sc) {
        System.out.println("=========欢迎您进入取款界面=========");
        double money = acc.getMoney();
        while (true) {
            // 输入取钱的金额
            System.out.println("请您输入取钱的金额：");
            double drawMoney = sc.nextDouble();
            // 判断取钱金额是否超过了当次限额
            if (drawMoney > acc.getQuotaMoney()) {
                System.out.println("您当前取款金额超过了每次限额！");
            } else {
                // 判断当前取钱金额是超过了账户的余额
                if (drawMoney > money) {
                    System.out.println("余额不足！当前余额是：" + money);
                } else {
                    // 更新账户余额
                    acc.setMoney(money - drawMoney);
                    System.out.println("取钱完成，请拿走您的钱，当前剩余余额是：" + acc.getMoney());
                    break;
                }
            }
        }
    }

    //存款
    private static void depositMoney(Account acc, Scanner sc) {
        System.out.println("欢迎您进入存款界面");
        System.out.println("请您输入存款金额：");
        double money = sc.nextDouble();
        acc.setMoney(acc.getMoney() + money);
        showAccount(acc);
    }

    //显示账户信息
    private static void showAccount(Account acc) {
        System.out.println("您当前账户信息如下");
        System.out.println("卡号：" + acc.getCardId());
        System.out.println("户主：" + acc.getUserName());
        System.out.println("余额：" + acc.getMoney());
        System.out.println("当次取现额度：" + acc.getQuotaMoney());
    }
    //转账
    private static void transferMoney(Account acc, ArrayList<Account> accounts, Scanner sc) {
        // 判断自己的账户中是否有钱
        if (acc.getMoney() <= 0) {
            System.out.println("余额为0，无法转账");
            return;
        }
        // 判断总账户数量是否大于等于2个。
        if (accounts.size() >= 2) {
            while (true) {
                //让当前用户输入对方的账号进行转账
                System.out.println("请您输入对方卡号");
                String cardId = sc.next();
                //根据卡号查询出集合中的账户对象
                Account otherAcc = getAccountByCardId(cardId, accounts);
                //判断账户对象是否存在，而且这个账户对象不能是自己
                if (otherAcc != null) {
                    //判断当前账户是否是自己。
                    if (acc.getCardId().equals(otherAcc.getCardId())) {
                        System.out.println("不能给自己账户转账，请重试");
                    } else {
                        String rs = "*" + otherAcc.getUserName().substring(1);
                        System.out.println("请您输入[" + rs + "]的姓氏来确认！");
                        System.out.println("请您输入对方的姓氏：");
                        String preName = sc.next();
                        if (otherAcc.getUserName().startsWith(preName)) {
                            // 认证通过
                            while (true) {
                                System.out.println("请您输入转账的金额（您最多可以转账：" + acc.getMoney() + "元）：");
                                double money = sc.nextDouble();
                                if (money > acc.getMoney()) {
                                    System.out.println("余额不足，请重试");
                                } else {
                                    //转账
                                    acc.setMoney(acc.getMoney() - money);
                                    otherAcc.setMoney(otherAcc.getMoney() + money);
                                    System.out.println("您已经完成转账！您当前还剩余：" + acc.getMoney());
                                    return;
                                }
                            }
                        } else {
                            System.out.println("您输入对方的信息有误！");
                        }
                    }
                } else {
                    System.out.println("您输入的卡号不存在！");
                }
            }
        } else {
            System.out.println("当前系统无其它账户可以转账，请重试");
        }
    }
    //修改密码
    private static void updatePassword(Account acc, Scanner sc) {
        // 判断旧密码是否正确
        while (true) {
            System.out.println("请您输入当前密码：");
            String password = sc.next();
            if (acc.getPassWord().equals(password)) {
                while (true) {
                    // 输入新密码
                    System.out.println("请您输入新密码：");
                    String newPassword = sc.next();
                    System.out.println("请您确认新密码：");
                    String okPassword = sc.next();
                    // 比对两次密码是否一致
                    if (newPassword.equals(okPassword)) {
                        acc.setPassWord(okPassword);
                        System.out.println("密码已修改，请重新登录！");
                        return;
                    } else {
                        System.out.println("两次输入密码不一致！");
                    }
                }
            } else {
                System.out.println("您输入的密码有误，请重新输入密码");
            }
        }
    }

}
